package com.wanmao.test;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import nari.mip.core.msg.MsgBase;


public class PushMessageBroadcastReceiver extends BroadcastReceiver {  
  

    @Override  
    public void onReceive(Context context, Intent intent) {  
    	
        String pushmessage = intent.getExtras().getString("pushmessage");  

        
        
        if (pushmessage.contains("mip_cmd_msg")) {// 第三方推送命令

			String startStr = "\"mip_cmd_msg:";
			String endStr = "}\"";
			int start = pushmessage.indexOf(startStr);
			int end = pushmessage
					.indexOf(endStr, start + startStr.length());
			String msg = pushmessage.substring(start + startStr.length(),
					end+1);


			MsgBase msgBase =null;
			
			try{
				

				msgBase = MsgBase.fromJSONString(msg);
				
				
			
			}catch(Exception e){
				
				e.printStackTrace();
			}
			
			if(null!=msgBase)
				showPushNotification(context,msgBase.toJSONString());
			

		}else{//管理服务网页推送推送消息
			

			String startStr = "\"title\":\"";
			String endStr = "\"}";
			int start = pushmessage.indexOf(startStr);
			int end = pushmessage
					.indexOf(endStr, start + startStr.length());
			String msg = pushmessage.substring(start + startStr.length(),
					end);
			
			showPushNotification(context,msg);
		}
       
    }  
    
    

  private void showPushNotification(Context context, String PushMessage)
    {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);

        Notification.Builder builder1 = new Notification.Builder(context);
        CharSequence tickerText = "ssologin.dependconsole推送通知";
        CharSequence contentTitle = "ssologin.dependconsole通知";
        long when = System.currentTimeMillis();
        builder1.setSmallIcon(R.mipmap.ic_launcher); //设置图标
        builder1.setTicker(tickerText);
        builder1.setContentTitle(contentTitle); //设置标题

        CharSequence contentText = PushMessage ;
        builder1.setContentText(contentText); //消息内容

        builder1.setWhen(System.currentTimeMillis()); //发送时间
        builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音，振动方式，灯光
        builder1.setAutoCancel(true);//打开程序后图标消失
        Intent intent =new Intent (context,TopMessageReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder1.setContentIntent(pendingIntent);
        Notification notification1 = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            notification1 = builder1.build();
        }
        mNotificationManager.notify(1, notification1); // 通过通知管理器发送通知
        
    }
   
}
